package cn.afactory.tygz.ui.base.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.controller.CommunityController;
import cn.afactory.tygz.model.AdInfo;
import cn.afactory.tygz.model.AdInfoList;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.activities.TopicAddActivity;
import cn.afactory.tygz.ui.activities.TopicHotListActivity;
import cn.afactory.tygz.ui.base.adapter.CommunityListAdapter;
import cn.afactory.tygz.ui.base.entity.BannerInfoList;
import cn.afactory.tygz.ui.base.entity.CommunityInfo;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshBase;
import cn.afactory.tygz.ui.base.thirdpart.pulltorefresh.PullToRefreshListView;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.ViewUtils;
import cn.afactory.tygz.ui.base.views.BannerView;
import cn.afactory.tygz.ui.base.views.CommunityGridView;
import cn.afactory.tygz.ui.base.views.search.SearchNavigationView;
import cn.afactory.tygz.ui.base.views.search.SearchSourcePager;
import cn.afactory.tygz.ui.fragments.BaseTabFragment;


public class CommunityFragment extends BaseTabFragment implements BannerView.OnBannerClickListener{

    private static final String TAG = CommunityFragment.class.getSimpleName();

    private CommunityListAdapter mAdapter;
    private BannerView mBannerView;
    private CommunityGridView mCommunityGridView;

    private CommunityController mController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mController == null) {
            mController = new CommunityController(mContext, mUIHandler, null);
        }

        initAd();
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case CommunityController.MSG_GET_AD_LIST_OK:
                if (msg.arg1 != 0) {

                } else {
                    if (msg.obj instanceof AdInfoList) {
                        onBannerDataLoaded((AdInfoList)msg.obj);
                    }
                }
                break;
            case CommunityController.MSG_GET_AD_LIST_KO:
                break;
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadDataFromCashe();
        startLoadData();
    }

//    @Override
//    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
//        View view = inflater.inflate(R.layout.fg_community, null);
//        initView(view);
//        return view;
//    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_community, null);
        initView(view);

        setTitle("社区");
        setRightButton("创建话题", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TopicAddActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

//    @Override
//    public void onTitleBarCreate(){
//        setTitleText("社区");
//    }

    private void startLoadData() {
        mController.loadBannerData();
    }

    /**
     * 加载banner数据完成
     *
     * @param adInfoList
     */
    private void onBannerDataLoaded(AdInfoList adInfoList) {
        mBannerView.setBannerList(adInfoList);
        mBannerView.setOnBannerClickListener(this);
    }

    private void loadDataFromCashe() {
        mController.loadDataFromCache();
    }

    private void initView(View view) {
        plistview = (PullToRefreshListView)view.findViewById(R.id.commuinty_list);
        mAdapter = new CommunityListAdapter(mContext, generateCommunityInfoList());
        plistview.setAdapter(mAdapter);
        plistview.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        createSearchView();
        mBannerView = new BannerView(mContext, null, null);
        plistview.getRefreshableView().addHeaderView(mBannerView, null, false);
        plistview.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView lv = plistview.getRefreshableView();
                if (position >= lv.getHeaderViewsCount()) {
                    position -= lv.getHeaderViewsCount();
                }
                CommunityInfo communityInfo = (CommunityInfo)mAdapter.getItem(position);
                switch (communityInfo.tag) {
                    case 0:
                        Intent intent = new Intent(mContext, TopicHotListActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
            }
        });
        createCommunityGridView();

        initCommunityListView();
    }

    private List<CommunityInfo> generateCommunityInfoList() {
        List<CommunityInfo> communityInfoList = new ArrayList<CommunityInfo>();

        CommunityInfo communityInfoA = new CommunityInfo();
        communityInfoA.tag = 0;
        communityInfoA.icon = R.drawable.community_hot_topic;
        communityInfoA.name = "热门话题";
        communityInfoList.add(communityInfoA);
        CommunityInfo communityInfoB = new CommunityInfo();
        communityInfoB.tag = 1;
        communityInfoB.icon = R.drawable.community_age_topic;
        communityInfoB.name = "你晒照，我来猜年龄";
        communityInfoList.add(communityInfoB);
        CommunityInfo communityInfoC = new CommunityInfo();
        communityInfoC.tag = 2;
        communityInfoC.icon = R.drawable.community_yue_topic;
        communityInfoC.name = "你敢晒，我敢约";
        communityInfoList.add(communityInfoC);
        return communityInfoList;
    }

    private void createCommunityGridView() {

        List<CommunityInfo> communityInfoList = new ArrayList<CommunityInfo>();
        CommunityInfo canYin = new CommunityInfo();
        canYin.name = "餐饮";
        canYin.icon = R.drawable.canyin;
        communityInfoList.add(canYin);
        CommunityInfo gongYu = new CommunityInfo();
        gongYu.name = "人才公寓";
        gongYu.icon = R.drawable.gongyu;
        communityInfoList.add(gongYu);
        CommunityInfo wuPin = new CommunityInfo();
        wuPin.name = "二手物品";
        wuPin.icon = R.drawable.wupin;
        communityInfoList.add(wuPin);
        CommunityInfo yuLe = new CommunityInfo();
        yuLe.name = "娱乐场所";
        yuLe.icon = R.drawable.yule;
        communityInfoList.add(yuLe);
        CommunityInfo jingDian = new CommunityInfo();
        jingDian.name = "周边景点";
        jingDian.icon = R.drawable.jingdian;
        communityInfoList.add(jingDian);
        CommunityInfo fuWu = new CommunityInfo();
        fuWu.name = "生活服务";
        fuWu.icon = R.drawable.fuwu;
        communityInfoList.add(fuWu);

        mCommunityGridView = new CommunityGridView(mContext, null, communityInfoList);
        plistview.getRefreshableView().addHeaderView(mCommunityGridView, null, false);
    }

    private void createSearchView() {
        View view = View.inflate(mContext, R.layout.view_search_com_layout, null);
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
        SearchNavigationView snv = ViewUtils.findView(view, R.id.search_view);
        snv.setLayoutParams(layoutParams);
        snv.setSearchSourcePager(SearchSourcePager.TOPIC);
        plistview.getRefreshableView().addHeaderView(snv, null, false);
    }

    private void initCommunityListView() {

    }

    /**
     * 加载广告
     */
    private void initAd(){
        mController.doLoadCommAds();
    }

    @Override
    public void onBannerClick(AdInfo adInfo) {
        if (adInfo.advType == 1) {
            //话题
            NaviUtils.gotoTopicDetailActivity(mContext, adInfo.topicId);
        } else {
            //url
            NaviUtils.gotoWebView(mContext, adInfo.link);
        }
    }
}
